<!--
 * @Author: 噜噜雯雯 2199987619@qq.com
 * @Date: 2025-06-21 16:36:53
 * @LastEditors: 噜噜雯雯 2199987619@qq.com
 * @LastEditTime: 2025-09-18 11:31:37
 * @FilePath: \linePatrol\vue-element-plus-admin\src\views\EquipManage\EquipType\index.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<script setup lang="tsx">
import { ContentWrap } from '@/components/ContentWrap'
import { useI18n } from '@/hooks/web/useI18n'
import { Table } from '@/components/Table'
import { ref, unref, reactive } from 'vue'
import { useTable } from '@/hooks/web/useTable'
import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
import { useDictStore } from '@/store/modules/dictData'
import { BaseButton } from '@/components/Button'
import { Dialog } from '@/components/Dialog'
import { tbUserGasdev } from '@/api/securcheck/type'
import { Search } from '@/components/Search'
import { ElImage } from 'element-plus'
import SecurChecktbUserGasdevApi from '@/api/securcheck/tbUserGasdev'
import Detail from './components/Detail.vue'
const { t } = useI18n()
const DictStore = useDictStore()
const PATH_URL = import.meta.env.VITE_API_PHOTO_PATH
//获取字典
DictStore.setDict('secur_mount_pos') //厂商类型
DictStore.setDict('secur_dev_state')
DictStore.setDict('secur_y_n') //气代煤用户
DictStore.setDict('secur_wh_category') //气代煤用户
DictStore.setDict('secur_sc_category') //气代煤用户
DictStore.setDict('secur_alarm_category') //气代煤用户
//表格内容接口
const { tableRegister, tableState, tableMethods } = useTable({
  fetchDataApi: async () => {
    const { pageSize, currentPage } = tableState
    const i = await SecurChecktbUserGasdevApi.GettbUserGasdev({
      pageIndex: unref(currentPage),
      pageSize: unref(pageSize),
      ...unref(searchParams)
    })
    return {
      list: i.rows,
      total: i.total
    }
  }
})
const { total, loading, dataList, pageSize, currentPage } = tableState
const { getList } = tableMethods
//表格结构
const trackFile: any = ref([])
const crudSchemas = reactive<CrudSchema[]>([
  {
    field: 'selection',
    type: 'selection',
    form: {
      hidden: true
    },
    detail: {
      hidden: true
    },
    fixed: 'left',
    search: { hidden: true }
  },
  {
    field: 'index',
    label: t('tableDemo.index'),
    fixed: 'left',
    type: 'index',
    search: {
      hidden: true
    },
    form: {
      hidden: true
    },
    detail: {
      hidden: true
    }
  },

  {
    field: 'gasdevType',
    label: t('设备名称'),
    search: {
      hidden: false,
      component: 'Input',
      componentProps: {
        placeholder: '设备名称'
      }
    },
    form: {
      colProps: {
        span: 22
      }
    }
  },
  {
    field: 'isInstall',
    label: t('是否安装'),
    search: {
      hidden: true
    },
    formatter: (_: Recordable, __: any, cellValue: string) => {
      return DictStore.$state.secur_y_n?.map((item) => {
        if (item.dictValue == cellValue) {
          return t(item.dictLabel)
        }
      })
    },
    detail: {
      slots: {
        default: (data: any) => {
          return DictStore.$state.secur_y_n?.map((item) => {
            if (item.dictValue == data.isInstall) {
              return t(item.dictLabel)
            }
          })
        }
      }
    }
  },
  {
    field: 'isExpire',
    label: t('是否过期'),
    search: {
      hidden: true
    },
    formatter: (_: Recordable, __: any, cellValue: string) => {
      return DictStore.$state.secur_y_n?.map((item) => {
        if (item.dictValue == cellValue) {
          return t(item.dictLabel)
        }
      })
    },
    detail: {
      slots: {
        default: (data: any) => {
          return DictStore.$state.secur_y_n?.map((item) => {
            if (item.dictValue == data.isExpire) {
              return t(item.dictLabel)
            }
          })
        }
      }
    }
  },
  {
    field: 'installDate',
    label: t('安装时间'),
    search: {
      hidden: true
    }
  },
  {
    field: 'useAge',
    label: t('使用年限'),
    search: {
      hidden: true
    }
  },
  {
    field: 'brand',
    label: t('品牌'),
    search: {
      hidden: true
    }
  },
  {
    field: 'mountPos',
    label: t('安装位置'),
    search: {
      component: 'Select',
      componentProps: {
        placeholder: '安装位置',
        options: DictStore.$state.secur_mount_pos?.map((item) => {
          return {
            label: item.dictLabel,
            value: item.dictValue
          }
        })
      }
    },
    formatter: (_: Recordable, __: any, cellValue: string) => {
      return DictStore.$state.secur_mount_pos?.map((item) => {
        if (item.dictValue == cellValue) {
          return t(item.dictLabel)
        }
      })
    },
    detail: {
      slots: {
        default: (data: any) => {
          return DictStore.$state.secur_mount_pos?.map((item) => {
            if (item.dictValue == data.mountPos) {
              return t(item.dictLabel)
            }
          })
        }
      }
    }
  },
  {
    field: 'devState',
    label: t('设备状态'),
    search: {
      component: 'Select',
      componentProps: {
        placeholder: '设备状态',
        options: DictStore.$state.secur_dev_state?.map((item) => {
          return {
            label: item.dictLabel,
            value: item.dictValue
          }
        })
      }
    },
    formatter: (_: Recordable, __: any, cellValue: string) => {
      return DictStore.$state.secur_dev_state?.map((item) => {
        if (item.dictValue == cellValue) {
          return t(item.dictLabel)
        }
      })
    },
    detail: {
      slots: {
        default: (data: any) => {
          return DictStore.$state.secur_dev_state?.map((item) => {
            if (item.dictValue == data.devState) {
              return t(item.dictLabel)
            }
          })
        }
      }
    }
  },
  {
    field: 'remark',
    label: t('备注'),
    search: {
      hidden: true
    },
    table: {
      hidden: true
    },
    form: {
      colProps: {
        span: 22
      }
    }
  },
  {
    field: 'file',
    label: '附件',
    table: {
      hidden: true
    },
    detail: {
      slots: {
        default: (data: any) => {
          const res = data.file ? JSON.parse(data.file) : []
          return res?.map((item: string) => {
            return (
              <ElImage
                style="width: 70px; height: 70px;margin-left:10px"
                src={PATH_URL + item}
                preview-src-list={[PATH_URL + item]}
                fit="cover"
              />
            )
          })
        }
      }
    },
    search: {
      hidden: true
    }
  },
  {
    field: 'action',
    label: t('tableDemo.action'),
    fixed: 'right',
    search: {
      hidden: true
    },
    form: {
      hidden: true
    },
    detail: {
      hidden: true
    },
    table: {
      slots: {
        default: (data: any) => {
          return (
            <>
              <span class="TableBtn" onClick={() => action(data.row)}>
                {t('查看')}
              </span>
            </>
          )
        }
      }
    }
  }
])

const { allSchemas } = useCrudSchemas(crudSchemas)
const currentRow = ref<tbUserGasdev>()
const dialogVisible = ref(false)

//表格中操作
const action = async (row: tbUserGasdev) => {
  trackFile.value = []
  dialogVisible.value = true //打开弹窗
  const res = await SecurChecktbUserGasdevApi.GettbUserGasdevById(row.uuid)
  currentRow.value = res
}
const searchParams = ref({})
const setSearchParams = (params: any) => {
  searchParams.value = params
  getList()
}
</script>
<template>
  <div class="flex w-100% h-100%">
    <ContentWrap class="flex-[3] ml-20px" title="燃气设备台账">
      <Search
        :schema="allSchemas.searchSchema"
        @search="setSearchParams"
        @reset="setSearchParams"
      />
      <Table
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :columns="allSchemas.tableColumns"
        :data="dataList"
        row-key="uuid"
        :loading="loading"
        @register="tableRegister"
        :pagination="{
          total
        }"
      />
    </ContentWrap>
    <Dialog v-model="dialogVisible" title="查看详情">
      <Detail :detail-schema="allSchemas.detailSchema" :current-row="currentRow" />
      <template #footer>
        <BaseButton @click="dialogVisible = false">{{ t('关闭') }}</BaseButton>
      </template>
    </Dialog>
  </div>
</template>
<style scoped>
:deep(.el-radio-button--small .el-radio-button__inner) {
  padding: 7px 15px !important;
}
.el-tree-node:focus > .el-tree-node__content {
  color: #409eff;
}
.el-card__body {
  padding: 10px;
  /* height: 90%; */
}
.CloseBtn {
  font-weight: 600;
  margin: 0 3px;
  color: darkgrey;
}
.AddbtnStyle {
  margin-bottom: 18px;
  margin-right: 10px;
}
.el-collapse-item {
  padding: 15px 0;
}
.el-collapse-item__content {
  padding-bottom: 0;
}
.el-collapse-item__header {
  width: 62px;
  height: 24px;
  border-radius: 2px;
  color: white;
  background: #409eff;
  justify-content: center;
  font-size: 12px;
}
.el-collapse-item__arrow {
  margin: 0 2px;
}

:deep(.TableBtn) {
  margin: 0 5px;
  cursor: pointer;
  color: #409eff;
  font-weight: 600;
}
</style>
